﻿using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ThreadLearn
{
    internal sealed class Type1 
    {
        public string Result { get; set; }
    }
    internal sealed class Type2 
    {
        public string Result { get; set; }
    }

    internal static class AsyncDemo
    {
        public static void Index()
        {
            Console.WriteLine("start");
            
            MyMethodAsync(1500);

            Console.WriteLine("main thread");

            
        }

        private static async Task<String> IssueClientRequestAsync(String serverName,String messages)
        {
            using (var pipe = new NamedPipeClientStream(serverName, "PipeName", PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough))
            {
                pipe.Connect();
                pipe.ReadMode = PipeTransmissionMode.Message;

                //将数据异步发送给服务器
                Byte[] request = Encoding.UTF8.GetBytes(messages);
                await pipe.WriteAsync(request, 0, request.Length);

                //异步读取服务器的响应
                Byte[] response = new Byte[1000];
                int typesRead = await pipe.ReadAsync(response, 0, response.Length);
                return Encoding.UTF8.GetString(response);
            }
        }

        public static Task<Type1> getType1(string result)
        {
            Type1 type = new Type1() { Result = result };
            //Task<Type1> task = new Task<Type1>(() => type);
            return Task.Run<Type1>(() => type);
        }

        public static Task<Type2> getType2(string result)
        {
            Type2 type = new Type2() { Result = result };
            return Task.Run<Type2>(() => type);
        }

        private static async Task<Type1> Method1Async(string result)
        {
            return await getType1(result);
        }

        private static async Task<Type2> Method2Async(string result)
        {
            return await getType2(result);
        }

        private static async Task<String> MyMethodAsync(Int32 argument)
        {
            Int32 local = argument;
            try
            {
                Thread.Sleep(10000);
                Console.WriteLine("entry");

                Type1 result1 = await Method1Async("type1");

                Console.WriteLine("Type1=>" + result1.Result);

                for(int i=0;i<3;i++)
                {
                    Type2 result2 = await Method2Async(string.Format("type2-{0}", i));

                    Console.WriteLine("Type2=>" + result2.Result);
                }
                
                //AsyncTaskMethodBuilder
                //TaskAwaiter
                //IAsyncStateMachine
            }
            catch(Exception)
            {
                Console.WriteLine("Catch6667777");
            }
            finally
            {
                Console.WriteLine("Finally231118899");
            }
            return "Done";
        }


    }

   



}
